import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fkjslee
 * @date 2018/2/11 at 16:09
 * Copyright (c) 2018 fkjslee. All rights reserved.
 */

public class ToString {
    public static void main(String[] args) {
        String result = getAllParamValue(new Parent());
        System.out.println("result : \t" + result);
        HashMap map = new HashMap();
        map.put("1", 11);
        map.put("2", 22);
        map.put("3", 33);
        map.put("4", "44");
        System.out.println(getAllParamValue(map));
    }

    public static String getAllParamValue(Object obj) {
        System.out.println("obj name = \t" + obj.getClass().getTypeName());
        Field[] fields = obj.getClass().getDeclaredFields();
        if (fields.length == 0) return "";
        StringBuilder builder = new StringBuilder("");
        builder.append(obj.getClass().getName()).append("\t:{\t");
        try {
            fields[0].setAccessible(true);
            if(fields[0].get(obj) == null) {
                builder.append(fields[0].getName()).append(": null");
            } else {
                if (isPrimitive(fields[0].get(obj).getClass().getTypeName())) {
                    builder.append(fields[0].getName()).append(": ").append(fields[0].get(obj));
                } else {
                    builder.append(getAllParamValue(fields[0].get(obj)));
                }
            }
            for (int i = 1; i < fields.length; ++i) {
                fields[i].setAccessible(true);
                if(fields[i].get(obj) == null) {
                    builder.append(fields[i].getName()).append(": null");
                }
                else {
                    if (isPrimitive(fields[i].get(obj).getClass().getTypeName())) {
                        builder.append(", ").append(fields[i].getName()).append(": ").append(fields[i].get(obj));
                    } else {
                        builder.append(", ").append(getAllParamValue(fields[i].get(obj)));
                    }
                }
            }
            builder.append("}");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }

    public static boolean isPrimitive(String name) {
        name = name.substring(name.lastIndexOf(".") + 1);
        if (name.equals("Boolean") || name.equals("boolean")) return true;
        if (name.equals("Character") || name.equals("char")) return true;
        if (name.equals("Byte") || name.equals("byte")) return true;
        if (name.equals("Short") || name.equals("short")) return true;
        if (name.equals("Integer") || name.equals("int")) return true;
        if (name.equals("Long") || name.equals("long")) return true;
        if (name.equals("Float") || name.equals("float")) return true;
        if (name.equals("Double") || name.equals("double")) return true;
        return name.equals("String") || name.equals("Void");
    }

    static class Parent {
        Integer param1 = null;
        Son son = new Son();
        Integer param2 = 2;
    }

    static class Son {
        Integer param3 = 3;
        Integer param4 = 4;
    }

}
